var e = getApp(), t = null;

Page({
    data: {
        time: 60,
        sended: !1,
        phone: "",
        authcode: ""
    },
    onLoad: function(e) {},
    onReady: function() {},
    onShow: function() {},
    onHide: function() {},
    onUnload: function() {},
    onPullDownRefresh: function() {},
    onReachBottom: function() {},
    onShareAppMessage: function() {},
    bindInputPhone: function(e) {
        var t = e.detail.value;
        11 === t.length && this.setData({
            phone: t
        });
    },
    bindInputAuthCode: function(e) {
        var t = e.detail.value;
        4 === t.length && this.setData({
            authcode: t
        });
    },
    refreshUserProfile: function() {
        e.getRequest(e.globalData.BASE_URL + "/auth/profile", {}).then(function(t) {
            console.log("userInfo", t), e.globalData.userInfo = t, wx.setStorageSync("userInfo", t), 
            e.globalData.campaign && e.campaignEventLog("signin"), wx.reLaunch({
                url: "/pages/stores/stores",
                success: function(e) {},
                fail: function(e) {},
                complete: function(e) {}
            });
        });
    },
    sendCodeHandler: function() {
        var t = this;
        if (this.data.phone) {
            e.globalData.code;
            e.postRequest(e.globalData.BASE_URL + "/auth/mobile/verify", {
                mobile: this.data.phone,
                country_code: "86"
            }).then(function(e) {
                t.setData({
                    sended: !0
                }), t.startTimer();
            });
        }
    },
    startTimer: function() {
        var e = this;
        t = setInterval(function() {
            e.data.time > 0 ? (e.data.time -= 1, e.setData({
                time: e.data.time
            })) : (e.setData({
                sended: !1,
                time: 6
            }), clearInterval(t));
        }, 1e3);
    },
    loginByPhone: function(t) {
        var n = this, o = t.detail.userInfo, a = this.data.phone, s = this.data.authcode, i = e.globalData.code;
        console.log("userInfo", o), a ? s ? e.postRequest(e.globalData.BASE_URL + "/auth/mobile/signin", {
            mobile: a,
            country_code: "86",
            code: s,
            session_code: i
        }).then(function(t) {
            e.globalData.token = t.token, e.globalData.userId = t.user_id, wx.setStorageSync("token", t.token), 
            wx.setStorageSync("userId", t.user_id), n.refreshUserProfile();
        }, function(t) {
            e.showErrorMsg("", "微信登录失败，请重试。"), setTimeout(function() {
                wx.reLaunch({
                    url: "/pages/stores/stores",
                    success: function(e) {},
                    fail: function(e) {},
                    complete: function(e) {}
                });
            }, 2e3);
        }) : e.showErrorMsg("验证码不能为空") : e.showErrorMsg("手机号不能为空");
    }
});